package com.moly.thrift.person;

import org.apache.thrift.TException;

import java.util.*;

public class PersonServiceHandler implements PersonService.Iface {
    @Override
    public Set<Person> getAllPersonNoRepeated() throws TException {

        System.out.println("测试 set");
        HashSet<Person> set = new HashSet<>();
        set.add(new Person().setId(new Random().nextLong())
                .setAge(new Random().nextInt())
                .setName("moly" + new Random().nextInt()));

        return set;


    }

    @Override
    public PersonMap getPersonByName(String name) throws TException {


        System.out.println("不支持 map 中 带 自定义对象 ");
        return new PersonMap().setName(name)
                .setPerson(new Person()
                        .setAge(new Random().nextInt()).setId(new Random().nextLong()).setName("moly"));


    }

    @Override
    public Map<String, String> test() throws TException {
        System.out.println("test   ");
        HashMap<String, String> map = new HashMap<>();
        map.put("a", "b");
        return map;
    }

    @Override
    public void say(String message) throws TException {

        System.out.println("客户端发送   " + message);
    }

    @Override
    public Person getPersonById(long id) throws TException {

        Person person = new Person();
        person.setAge(new Random().nextInt(10));
        person.setName("傻逼——" + new Random().nextInt());
        person.setId(id);
        return person;

    }

    @Override
    public List<Person> getPersonsByAge(int age) throws TException {

        ArrayList<Person> list = new ArrayList<>();
        list.add(new Person().setAge(age).setName("moly_" + new Random().nextInt()));
        list.add(new Person().setAge(age).setName("moly_" + new Random().nextInt()));
        list.add(new Person().setAge(age).setName("moly_" + new Random().nextInt()));
        return list;
    }


    @Override
    public boolean isExists(Person person) throws InvalidOperation, TException {

        if (person.getAge() > 10) {
            System.out.println("这家伙成人了 ");
            return true;
        }
        System.out.println("小屁孩 ");
        return false;
    }
}
